package com.ma.DataStruct.StackQueue;

import java.util.LinkedList;
import java.util.Queue;

/**
 * 用队列实现栈
 */
public class Solution225 {
}

/* 一个队列实现栈 */
class MyStack {

    private Queue<Integer> q;
    //记录栈顶元素
    private int top_item ;

    public MyStack() {
        q = new LinkedList<>();
        top_item = 0;
    }

    public void push(int x) {
        q.offer(x);
        top_item = x;
    }

    public int pop() {
        int size = q.size();
        while (size > 2) {
            q.offer(q.poll());
            size--;
        }
        top_item = q.peek();
        q.offer(q.poll());
        return  top_item;
    }


    public int top() {
        return top_item;
    }

    public boolean empty() {
        return q.isEmpty();
    }
}
